package com.homework.news

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView

/*
* 顶部导航栏适配器
* */
class TopNavAdapter(val items: List<String>): RecyclerView.Adapter<TopNavAdapter.ViewHolder>() {

    inner class ViewHolder(view: View) : RecyclerView.ViewHolder(view){
        val item: TextView = view.findViewById(R.id.item)
    }

    private var mOnItemClickListener: OnItemClickListener? = null

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val view = LayoutInflater.from(parent.context)
            .inflate(R.layout.nav_item, parent, false)
        return ViewHolder(view)
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        println("开始生成导航栏")
        val itemStr = items[position]
        holder.item.text = itemStr
        if(mOnItemClickListener!=null){
            var position = holder.layoutPosition
            holder.itemView.setOnClickListener {
                mOnItemClickListener!!.onItemClick(holder.itemView, position)
            }
        }
    }

    override fun getItemCount(): Int {
        return items.size
    }

    interface OnItemClickListener {
        fun onItemClick(view: View, position: Int)
    }

    fun setOnItemClickListener(onItemClickListener: OnItemClickListener) {
        this.mOnItemClickListener = onItemClickListener
    }

}